<template>
	<view class="root-view">
		<view class="device-layout-block">
			<view class="device-layout-mod">
				<view class="device-layout-mhead" v-if="false">
					<evan-steps :active="1" direction="horizontal" primaryColor="#428cff">
						<evan-step></evan-step>
						<evan-step></evan-step>
						<evan-step></evan-step>
					</evan-steps>
					 <!-- <u-steps :list="numList" :current="0" mode="number" active-color="#428cff"></u-steps> -->
				</view>
				<view class="device-layout-mbody">
					<view class="textinfo-box">
						<!-- <view class="textinfo-title" style="color:red">设备的MAC地址{{device_id}}</view> -->
						<!-- <view class="textinfo-title" style="color:red">正在配对的设备的MAC地址{{bssid}}</view> -->
						<view class="textinfo-title">请选择设备工作的WI-FI</view>
						<view class="textinfo-desc">此设备只支持家中使用2.4GHz Wi-Fi连接使用Wi-Fi名称仅支持英文字符及数字,请先确保手机端已连接2.4G的wifi</view>
					</view>
					<view class="uform-box u-p-b-64">
						<u-form class="u-border-top" :model="form" ref="uForm">
							<u-form-item left-icon="wifi">
								<u-input v-model="form.wifiName" type="text" :disabled="true" placeholder="请连接Wi-Fi" />
							</u-form-item>
							<u-form-item left-icon="lock-fill">
								<u-input v-model="form.wifiPassword" type="password" placeholder="输入WIFI密码" />
							</u-form-item>
						</u-form>
					</view>
					
				</view>
				<view class="device-layout-mfoot ">
					<view class="device-layout-btns">
						<view class="btn-cell">
							<button class="coco-btn theme round lg shadow" @click="setWifi();" :disabled="disabled">开始配网</button>
						</view>
					</view>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import evanStep from '@/components/evan-steps/evan-step.vue';
	export default {
		components:{
			evanStep
		},
		data() {
			return {
				numList: [{
					name: '滤芯'
				}, {
					name: 'WIFI'
				}, {
					name: '设备'
				} ],
				form: {
					wifiName: '',
					wifiPassword: '',
				},
				gbWifi:null,
				bssid:'',
				disabled:false,
				device_id:''
			};
		},
		onLoad(opt) {
			this.device_id = opt.device_id ? opt.device_id : '';
			this.gbWiFi = uni.requireNativePlugin('Gl-WiFi'); 
			this.getWifiName();
		},
		methods:{
			jump(path) {
				this.$tools.routerTo(path);
			},
			getWifiName(){
				this.gbWiFi.getWiFiName((res) => {
					 console.log(res);
					 if(res.code == 1){
						 if(res.is5G){
							 this.disabled = true;
							 this.$u.toast("当前连接的是5G的WIFI，请切换到2.4G后重试！");
						 }
						 else{
							 this.disabled = false;
							 this.form.wifiName = res.ssid;
							 this.bssid = res.bssid;
							 // if(this.device_id){
								//  this.bssid = this.device_id;
							 // }
							 // if(this.device_id != '' && this.bssid != this.device_id){
								//  uni.showModal({
								//  	title:"系统提醒",
								//  	content:'您选择的设备与网络中的设备不是一台，无法继续，请选择正确的设备',
								//  	showCancel:false,
								// 	success: (ress) => {
								// 		if(ress.confirm){
								// 			uni.navigateBack();
								// 		}
								// 	}
								//  })
							 // }
						 }
					 }
					 else{
						 this.disabled = true;
						 this.$u.toast(res.result);
					 }
				})
			},
			setWifi(){
				// if(this.disabled){
				// 	return ;
				// }
				// console.log(this.disabled);
				uni.showLoading({
					title:"正在配网，请稍后...."
				});
				// this.bindDevice('d2:8d:0d:80:10:ca');
				this.gbWiFi.startEsptouch(
					{
				        ssid: this.form.wifiName,
				        pwd: this.form.wifiPassword,
				        broadcast: 1,
						bssid:this.bssid
				    },
				    result => {
						uni.hideLoading();
						console.log(result);
						if(result.code == 1){
							this.bindDevice(result);
						}
						else{
							this.$u.toast(result.result);
						}
				});
			},
			bindDevice(result){
				result.device_id = this.device_id;
				this.$post("api/member/bind_equipment",result).then(res => {
					if(res.code == 1){
						this.$u.toast("恭喜你，设备配网并绑定成功！");
						setTimeout(()=>{
							this.$Router.back();
						},2000);
					}
					else{
						this.$u.toast(res.msg);
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "./css/device.scss";
	 
</style>
